<?php

class Model_Message extends Database
{	

	function Model_Message()
	{
		$this->msg  = MESSAGE;
		$this->user = USERS;
		$this->msglabel  = MESSAGELABEL;
		$this->Database();
	}
	
	function addMessage($adArray)
	{
		$this->InsertData( $this->msg , $adArray );		
		$insertId = mysql_insert_id();
		return $insertId;
	}
	
	
	## Edit user by userid
	function editMessageValueById($array, $Id){
		$this->UpdateData($this->msg,$array,"id",$Id,0);
	}
	
	function deleteMessageValueById($Id) {
			$this->DeleteData($this->msg,"id",$Id);
	}	
	
	function getMessageDetailsById($id) {
		$fields=array();	
		$tables=array($this->msg);
		$where=array("id=".$id);
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	#get all recieved message
	function getAllMessage($searchtxt='',$fromname='',$toname='',$col1='',$limit='',$offset='') {
		$fields=array("$this->msg.*");	
		$tables=array($this->msg );
		$where = array("((label='inbox' and to_id='1' ) or (label='outbox' and from_id='1' ))");
		if($searchtxt!='')
		{
			$where[]= " (subject LIKE '%".$searchtxt."%' )";	
		}
		if($fromname!='')
		{
			$where[]= " (from_id = '".$fromname."' )";	
		}
		if($toname!='')
		{
			$where[]= " (to_id ='".$toname."' )";	
		}
		if($col1=='0')
		{
			$order = array("send_date asc");
		}
		if($col1=='1')
		{
			$order = array("send_date desc");
		}
		if($col1=='')
		{
			$order = array("id desc");
		}
		
		
		$result1 = $this->SelectData($fields,$tables, $where, $order, $group=array(""),$limit,$offset,0); 
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	#get all recieved message
	function getAllMessageByToId($to_id,$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->msg);
		$where=array("to_id=".$to_id,"label='inbox'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("send_date desc"), $group=array(),$limit,$offset,0); 
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	#get count of unread recieved message
	function getCountUnreadMessageByToId($to_id) {
		$fields=array("count(*) as cnt");	
		$tables=array($this->msg);
		$where=array("to_id=".$to_id,"status='unread'","label='inbox'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit,$offset,0); 
		$result= $this->FetchRow($result1); 
		return $result['cnt'];		
	}
	
	#get all sent message
	function getAllMessageByFromId($from_id,$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->msg);
		$where=array("from_id=".$from_id,"label='outbox'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("send_date desc"), $group=array(),$limit,$offset,0); 
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	## Delete Message cat by id
	function deleteMessageById($Messageid){
		$this->DeleteData($this->msg,"id",$Messageid);
	}
	
	/****************************************************/
	## Update Message status with multiple ids
	function updateMultipleMessageStatus($Messageids, $status) {
		$sql = "UPDATE ".$this->msg." SET status='".$status."' WHERE id IN (".$Messageids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete Message category with multiple ids
	function deleteMultipleMessage($Messageids) {
		$sql = "DELETE FROM ".$this->msg." WHERE id IN (".$Messageids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Update Message status with Messageid
	function updateMessageStatus($Messageid, $status) {		
		$sql = "UPDATE ".$this->msg." SET status='".$status."' WHERE id = (".$Messageid.")";
		$result1= $this->ExecuteQuery($sql);	 
		
	}
	
	#get count of recieved message
	function getCountMessageByToId($to_id) {
		$fields=array("count(*) as cnt");	
		$tables=array($this->msg);
		$where=array("to_id=".$to_id,"label='inbox'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit,$offset,0); 
		$result= $this->FetchRow($result1); 
		return $result['cnt'];		
	}
	
	#get count of sent message
	function getCountSentMessageByToId($to_id) {
		$fields=array("count(*) as cnt");	
		$tables=array($this->msg);
		$where=array("from_id=".$to_id,"label='outbox'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit,$offset,0); 
		$result= $this->FetchRow($result1); 
		return $result['cnt'];		
	}
	
}
?>